import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-bom-hold',
	templateUrl: './bom-material.component.html',
	styleUrls: ['./bom-material.component.css']
})
export class BomMaterialComponent implements OnInit {
	tableData: any = []; // table的表单数据
	totalCounts: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	fileUp: string = '';
	fileToUpload: File = null; //上传文件回调参数
	// 查询参数
	bomNumber: string = ""; // BOM编码
	enterMan: string = ""; // 录入人
	startTime: any = ""; // 开始时间
	endTime: any = "";// 结束时间
	materielType: any = ""// 产品类型(数字)
	productName: string = "";// 产品名称
	materielNumber: string = "";// 物料编码

	validatorOptions = {
		excluded: [":disabled"],
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		},
		fields: {
			type: {
				trigger: "change",
				validators: {
					notEmpty: {
						message: '请选择物料类型'
					}
				}
			},
			productName: {
				validators: {
					notEmpty: {
						message: '请填写产品名称'
					}
				}
			},
			materialCode: {
				validators: {
					notEmpty: {
						message: '请输入物料编码'
					},
				}
			}
		}
	}
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}
	ngOnInit() {
		this.queryMaterial() //获取物料类型列表数据
		this.load();
	}

	//初始化加载list数据
	load() {
		let param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				materielType: this.materielType,	// 产品类型(数字)
				productName: this.productName, 	// 产品名称
				materielNumber: this.materielNumber,	// 物料编码
				enterMan: this.enterMan,		// 录入人
				startTime: this.startTime,	// 开始时间
				endTime: this.endTime 			// 结束时间
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("bomMaterialController/materielList.html", reqdata).subscribe(result => {
			if (result.isSuccess && result.exhibitDatas) {
				this.tableData = result.exhibitDatas
				this.totalCounts = result.recordCount;
			}
		});
	}
	// 删除输入
	delKey(type) {
		if (type == "productName") {
			this.productName = ""
		}
		if (type == "materielNumber") {
			this.materielNumber = ""
		}
		if (type == "enterMan") {
			this.enterMan = ""
		}
	}
	//时间选中回调
	datePickerSelected(time) {
		this.startTime = time.startTime;
		this.endTime = time.endTime;
	}
	// 查询
	tableSearch() {
		this.curPage = 1;
		this.load();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		this.load();
	}
	// 物料实体 往后台传递的
	material: any = {
		// id:"",
		materielType: "",
		productName: "",
		materielNumber: ""
	}
	// 选择物料类型
	typeChange(value) {
		this.material.materielType = value
	}
	// 添加Bom编码
	addMaterialCode(modal) {
		// this.clearData();
		modal.show();
		// $("#addBomCodeModal").modal("show")
	}


	// 显示导入的弹框
	showImportModal(modal) {
		modal.show();
	}
	// 关闭导入的弹框
	cancelUp(val) {
		val.hide();
		this.fileToUpload = null;
	}
	//下载模板
	load_module() {
		var elemIF = document.createElement("iframe");
		elemIF.src = config.BaseURL + 'resources/template/materiel-Template.xlsx';
		elemIF.style.display = "none";
		document.body.appendChild(elemIF);
	}
	// 清空input type=file输入框的值
	clearFile() {
		this.fileUp = '';
	}
	//文件上传change回调
	fileChange(files: FileList) {
		this.fileToUpload = files.item(0);
		const isEXCEL = this.fileToUpload.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.fileToUpload.type === "application/vnd.ms-excel";
		if (!isEXCEL) {
			this.jsonApiService.message('上传文件只能是 excel格式!');
			this.fileToUpload = null;
			return;
		}
	}
	//导入submit
	submitData(val) {
		let postParam = new FormData();
		postParam.append('file', this.fileToUpload);
		if (!this.fileToUpload) {
			this.jsonApiService.message('请上传文件')
			return false;
		}
		this.jsonApiService.form_http("bomMaterialController/uploadMaterielExcel.html", postParam).subscribe(data => {
			this.jsonApiService.message(data.message);
			if (data.success) {
				val.hide();
				this.fileToUpload = null;
				this.load();
			}
		})
	}


	// 更新dom编码
	updata_domCode(row, modal) {
		let obj = JSON.parse(JSON.stringify(row))
		this.material = obj;
		modal.show();
		// $("#addBomCodeModal").modal("show")
	}
	// 删除dom编码
	del_domCode(row) {
		this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
			let reqdata = 'id=' + row.id;
			this.jsonApiService.http("bomMaterialController/delMateriel.html", reqdata).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					this.load();
				}
			})
		})
	}
	// 刷新表
	refresh() {
		this.load();
	}
	// 关闭添加和编辑弹窗
	closeAddBomModal(modal) {
		$("#bomHoldFrom").data('bootstrapValidator').resetForm();
		modal.hide();
		// $("#addBomCodeModal").modal("hide")
		this.clearData()
	}
	clearData() {
		this.material = {
			materielType: "",
			productName: "",
			materielNumber: ""
		}
		delete this.material.id
		$("#bomHoldFrom").data('bootstrapValidator').resetForm();
	}
	// 提交添加的数据
	submitModal(modal) {
		$('#bomHoldFrom').bootstrapValidator('validate'); //开启验证
		let isValid = $('#bomHoldFrom').data('bootstrapValidator').isValid();  //验证是否通过
		if (isValid) {
			this.jsonApiService.http_obj("bomMaterialController/editOrInsertMateriel.html", this.material).subscribe(data => {
				this.jsonApiService.message(data.message);
				if (data.success) {
					// 
					this.load();
					this.closeAddBomModal(modal)
				}
			})
		}
	}

	materialTypeList: any = [] //物料类型列表数据
	//请求物料类型的接口
	queryMaterial() {
		this.jsonApiService.http('materialType/list.html', '').subscribe(data => {
			if (data.success) {
				this.materialTypeList = data.data
			}
		})
	}
}